package com.sctvcloud.renhe.ui.activities;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.renhe.R;
import com.sctvcloud.renhe.base.BaseActivity;
import com.sctvcloud.renhe.beans.FHistory;
import com.sctvcloud.renhe.beans.HighRiseNewsInfo;
import com.sctvcloud.renhe.beans.NewsItem;
import com.sctvcloud.renhe.http.AbsListNetCallback;
import com.sctvcloud.renhe.http.NetUtils;
import com.sctvcloud.renhe.ui.adapter.HighRiseNewsListAdapter;
import com.sctvcloud.renhe.ui.adapter.HistoriesAdapter;
import com.sctvcloud.renhe.ui.utils.TitleUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 高层新闻  更多
 */
public class HighRiseMoreActivity extends BaseActivity implements
        CanRefreshLayout.OnRefreshListener, CanRefreshLayout.OnLoadMoreListener {
    public static final String HIGH_RISE_MORE = "more_high_rise_news";
    @BindView(R.id.title_top_edit)
    protected CustomFontTextView edit;
    @BindView(R.id.can_content_view)
    protected RecyclerView mRecyclerView;
    @BindView(R.id.high_rise_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.hr_news_no)
    protected LinearLayout no_data;
    private HighRiseNewsListAdapter mAdapter;
    private String dataUrl;
    private int currentNum;
    private List<HighRiseNewsInfo> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this);
        initView();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_high_rise_more);
        ButterKnife.bind(this);
    }

    private void initView() {
        list = new ArrayList<>();
        dataUrl = getIntent().getStringExtra(HIGH_RISE_MORE);
        edit.setVisibility(View.GONE);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setLoadMoreEnabled(false);
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        if (mAdapter == null) {
            mAdapter = new HighRiseNewsListAdapter(this, list);
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.setDataList(list);
        }
        refreshLayout.autoRefresh();
    }


    @Override
    public void onLoadMore() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.loadMoreComplete();
        } else {
            requestData();
        }
    }

    @Override
    public void onRefresh() {
        list.clear();
        requestData();
    }

    /**
     * 请求高层新闻
     */
    private void requestData() {
        if (TextUtils.isEmpty(dataUrl)) {
            Toast.makeText(HighRiseMoreActivity.this, "数据错误", Toast.LENGTH_LONG).show();
            return;
        }
        NetUtils.getNetAdapter().getNewsArray(getOwnerName(), dataUrl, new AbsListNetCallback<NewsItem>(NewsItem.class) {
            @Override
            public void onSuc(List<NewsItem> li) {
                if (null != li && li.size() > 0) {
                    HighRiseNewsInfo info;
                    for (int i = 0; i < li.size(); i++) {
                        info = new HighRiseNewsInfo(1, "", li.get(i));
                        list.add(info);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                Toast.makeText(HighRiseMoreActivity.this, "请求失败", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onEnd() {
                onNetFinish();
                if (null != list && list.size() > 0) {
                    mAdapter.setDataList(list);
                    refreshLayout.setVisibility(View.VISIBLE);
                    no_data.setVisibility(View.GONE);
                } else {
                    refreshLayout.setVisibility(View.GONE);
                    no_data.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private synchronized void onNetFinish() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.refreshComplete();
        }
    }
}
